package com.chenfan.common.extension.util;

import com.chenfan.common.extension.util.wrapper.RunnableWrapper;
import com.chenfan.common.extension.util.wrapper.SupplierWrapper;

import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * 智能并行计算工具类
 * @author wulg
 * @date 2021-09-15
 **/
public final class SmartCompletableFuture {

    public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor){
        return CompletableFuture.runAsync(new RunnableWrapper(runnable), executor);
    }

    public static CompletableFuture<Void> runAsync(Runnable runnable){
        return CompletableFuture.runAsync(new RunnableWrapper(runnable));
    }

    public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier){
        return CompletableFuture.supplyAsync(new SupplierWrapper<>(supplier));
    }

    public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, Executor executor) {
        return CompletableFuture.supplyAsync(new SupplierWrapper<>(supplier), executor);
    }
    public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {
        return CompletableFuture.allOf(cfs);
    }

    public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {
        return CompletableFuture.anyOf(cfs);
    }
}
